package net.sf.rose.util;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author feng_jian
 * 
 */
public class ListProperty {

	private boolean updateFlag = false;
	private File file = null;
	private List<String> list = new ArrayList<String>();
	private Map<String, String> map = new HashMap<String, String>();

	/**
	 * 将所有数据读入内存
	 * 
	 * @throws IOException
	 */
	public void load(File file) throws IOException {
		this.file = file;
		int len = 1024;
		char[] buf = new char[len];
		try {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			int i = -1;
			int off = 0;
			int type = 0;// 0:注释;1:内容
			while ((i = in.read()) != -1) {
				char b = (char) i;
				if (b == '\n') {
					readLine(type, buf, off);
					type = 1;
					off = 0;
				} else if (b == '#') {
					type = 0;
					buf[off] = b;
					off++;
				} else {
					// 如果缓存已满就扩大一倍
					if (off == len) {
						len = len * 2;
						char[] buf2 = new char[len];
						for (int j = 0; j < buf.length; j++) {
							buf2[j] = buf[j];
						}
						buf = buf2;
					}
					buf[off] = b;
					off++;
				}
			}
		} catch (IOException e) {
			throw e;
		}
	}

	/**
	 * 读取一行数据
	 */
	private void readLine(int type, char[] buf, int off) {
		// 1:内容
		if (type == 1) {
			int index = 0;
			for (; index < off; index++) {
				if (buf[index] == '=') {
					break;
				}
			}
			String key = StringUtil.fromUnicode(buf, 0, index);
			String val = StringUtil.fromUnicode(buf, index + 1, off - (index + 2));
			list.add(key);
			if (map.get(key) != null) {
				System.out.println(key);
			}
			map.put(key, val);
		}
		// 0:注释
		else {
			list.add(StringUtil.fromUnicode(buf, 0, off));
		}
	}

	/**
	 * 保存
	 * 
	 * @throws IOException
	 */
	public void save() throws IOException {
		if (!updateFlag) {
			return;
		}
		try {
			BufferedWriter buf = new BufferedWriter(new FileWriter(file));
			for (String s : list) {
				if (s.charAt(0) == '#') {
					String s1 = StringUtil.toUnicode(s, false);
					buf.write(s1.substring(1, s1.length() - 2));
				} else {
					String key = StringUtil.toUnicode(s, false);
					String val = StringUtil.toUnicode(map.get(s), false);
					buf.write(key + '=' + val);
				}
				buf.newLine();
			}
			buf.flush();
			buf.close();
		} catch (IOException e) {
			throw e;
		}
	}

	/**
	 * 添加注释
	 */
	public void addComment(String comment) {
		list.add('#' + comment);
	}

	/**
	 * 设置属性
	 */
	public void set(String key, String value) {
		if (!list.contains(key)) {
			list.add(key);
		}
		map.put(key, value);
		updateFlag = true;
	}

	/**
	 * 设置属性
	 */
	public void insert(String ref, String key, String value) {
		if (!list.contains(key)) {
			list.add(list.indexOf(ref), key);
		}
		map.put(key, value);
		updateFlag = true;
	}

	/**
	 * 取得属性
	 */
	public String get(String key) {
		return map.get(key);
	}

	/**
	 * 删除属性
	 */
	public void remove(String key) {
		map.remove(key);
		list.remove(key);
		updateFlag = true;
	}

	/**
	 * 返回所有键值
	 */
	public Map<String, String> getMap() {
		return map;
	}

	/**
	 * 返回所有键值
	 */
	public List<String> getList() {
		return list;
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		ListProperty p = new ListProperty();
		// p.load(new File("hello.properties"));
		p.load(new File("conf/txt_zh_CN.properties"));
		System.out.println(p.get("affairmanager"));
		System.out.println(p.get("exit"));
		p.addComment("author:jian feng");
		// p.setProperty("language", "日语");
		// p.setProperty("dhcpmailcontent",
		// "局域网中有一台计算机名({0})MAC({1})地址的计算机申请获取IP地址。\n\n分配至受限来客用户组，请点击：
		// {2}sort=visitor1 \n\n分配至可上网来客用户组，请点击： {2}sort=visitor2 \n ");
		System.out.print(p.map.get("language"));
		System.out.print(p.map.get("dhcpmailcontent"));
		// p.save();
	}
}
